package com.example.shw

import android.app.AlarmManager
import android.app.PendingIntent
import android.app.TimePickerDialog
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import android.widget.Toast
import java.util.*

class AlarmActivity : AppCompatActivity(){
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val linearLayout = LinearLayout(this)
        val button = Button(this)
        button.setText("设置闹铃")
        linearLayout.addView(button)
        setContentView(linearLayout)
        button.setOnClickListener {
            val currentTime = Calendar.getInstance()
            TimePickerDialog(this@AlarmActivity, 0,
                TimePickerDialog.OnTimeSetListener{ _, hourOfDay, minute ->
                    val intent = Intent(this@AlarmActivity, AlarmTwoAcitvity::class.java)
                    val pi = PendingIntent.getActivity(this@AlarmActivity, 0, intent, 0)
                    val c = Calendar.getInstance()
                    c.timeInMillis = System.currentTimeMillis()
                    c.set(Calendar.HOUR, hourOfDay)
                    c.set(Calendar.MINUTE, minute)

                    val aManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
                    aManager.setExact(AlarmManager.RTC_WAKEUP, c.timeInMillis, pi)
                    Toast.makeText(this@AlarmActivity, "闹铃设置成功啦,hour:$hourOfDay;minute:$minute",
                        Toast.LENGTH_SHORT).show()
                }, currentTime.get(Calendar.HOUR_OF_DAY),
                currentTime.get(Calendar.MINUTE), false).show()
        }
    }
}